<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MyFile extends CI_Controller {

	public function __construct(){
		parent::__construct();
		if(!$this->session->userdata('user_name'))
			redirect('/');
	}
	
	public function index(){
		redirect('myfile/file');
	}
	
	public function file($offset=''){
		$data['title']		=	'My File';
		$data['content']	=	'myfile/mylist';
		$paging		 = 1;
		$data['myfile']		=	 $this->files->getAllFile($this->session->userdata('keyword'),$offset,$paging);
		$num_records = COUNT($this->files->getAllFile($this->session->userdata('keyword')));
		if ($num_records > $paging) {
        	    $this->load->library('pagination');
	            $config['base_url']   = site_url('myfile/file/');
	            $config['total_rows'] = $num_records;
	            $config['per_page']   = $paging;
	            $config['uri_segment']= 3;
	            $this->pagination->initialize($config);	 
	            $data['pagination']   = $this->pagination->create_links();
        }	
		$this->load->view('themes/index',$data);
	}
	
	public function edit($id='')
	{
		$data['title']		=	'Check Stok';
		$data['files']		=	$this->files->getDataFile($id);
		//$data['parent_dir']	=	$this->files->getAllParentDir();
		$data['content']	=	'myfile/myedit';
		$this->load->view('themes/index',$data);
	}
	
	function UpdateStok(){
        $this->db->select('box_sedang,sisa,box_kecil,stok');
		$this->db->where('id_obat',$this->input->post('id'));
		$Q=$this->db->get('vw_stok',1);
		$val=$Q->row_array();
		
		$big_box   = $val['box_sedang'] * $this->input->post('box_sedang');
		$small_box = $val['box_kecil'] * $this->input->post('box_kecil');
		$pcs 	   = $this->input->post('box_satuan');
		$stok_f    = $big_box+$small_box+$pcs;
		$stok_k	   = $val['sisa'];
		$stok_g	   = $val['stok'];	
		
		//update stok gudang
		$gudang = ($stok_f-$stok_k) + $stok_g;
		$value2 = array(
						'flag_opname' =>	1,
						'stok' 		  => $gudang	
					  );
		$this->db->where('id_obat',$this->input->post('id'));			  
		$this->db->update('tbl_obat',$value2);
		
		//insert opname
		$value = array(
						'id_obat'	 =>	$this->input->post('id'),
						'stok_sblm'	 =>	$stok_k,
						'kem_sedang' => $this->input->post('box_sedang'),
						'kem_kecil'  => $this->input->post('box_kecil'),
						'satuan' 	 => $this->input->post('box_satuan'),
						'id_pengguna'=> 1,
						'tgl_input'	 => '2012-12-20 11:00:00'	
					  );
		$this->db->insert('tbl_opname',$value);
		
		redirect('myfile/file');
	}
	
	public function do_upload(){
		$config['upload_path'] = './assets/member/'.$this->session->userdata('user_name');
		$config['allowed_types'] = 'xls|xlsx|pdf|doc|docx|txt|jpg|gif';
		$config['max_size']= 9999999;
		$config['overwrite']=true;
		$this->load->library('upload',$config);
		if (!$this->upload->do_upload())
		{
			$this->session->set_flashdata('error','error');
			echo $config['upload_path'];
		}
		else
		{
            $data = array('error' => false);
			$upload_data = $this->upload->data();
			$name	=	$_FILES['userfile']['name'];
			$type	=	$_FILES['userfile']['type'];
			$size	=	$_FILES['userfile']['size'];
			$folder =	$this->input->post('dir');
			$this->files->getSave($name,$type,$size);
			$this->session->set_flashdata('success','success');
            redirect('myfile/upload');
		}	
	}
	
	function new_folder(){
		if($this->files->getCheckFolder($this->input->post('folder'))>0):
			$this->session->set_flashdata('error','error');
			redirect('myfile/file');
		else:
			$name	=	$this->input->post('folder');
			$type   =   'application/folder';
			$size	=	'0';
			$this->files->getSave($name,$type,$size);
			$this->session->set_flashdata('success','success');
			redirect($this->input->server('HTTP_REFERER'));
		endif;	
	}
	
	public function listing($id='',$offset=''){
		$data['title']		=	'My File Result';
		$data['content']	=	'myfile/mylist';
		$paging		 		= 	10;
		$data['id']			=	$id;
		$data['myfile']		=	$this->files->getAllFolder($this->session->userdata('keyword'),$id,$offset,$paging);
		$num_records 		=   COUNT($this->files->getAllFolder($this->session->userdata('keyword'),$id));
		if ($num_records > $paging) {
        	    $this->load->library('pagination');
	            $config['base_url']   = site_url('myfile/listing/'.$id.'/');
	            $config['total_rows'] = $num_records;
	            $config['per_page']   = $paging;
	            $config['uri_segment']= 3;
	            $this->pagination->initialize($config);	 
	            $data['pagination']   = $this->pagination->create_links();
        }	
		$this->load->view('themes/index',$data);
	}
	
	public function remove($id=''){
		$this->files->getRemove($id);
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
}
